package com.flyou.ccnucenter.ui.activity.henuBeside.lostFind;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.ui.frgment.HenuBeside.lostFind.BagFragment;
import com.flyou.ccnucenter.ui.frgment.HenuBeside.lostFind.BookFragment;
import com.flyou.ccnucenter.ui.frgment.HenuBeside.lostFind.CardFragment;
import com.flyou.ccnucenter.ui.frgment.HenuBeside.lostFind.DigitFragment;
import com.flyou.ccnucenter.ui.frgment.HenuBeside.lostFind.OtherFragment;
import com.flyou.ccnucenter.utils.LoginUtils;
import com.mingle.entity.MenuEntity;
import com.mingle.sweetpick.BlurEffect;
import com.mingle.sweetpick.Delegate;
import com.mingle.sweetpick.RecyclerViewDelegate;
import com.mingle.sweetpick.SweetSheet;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;

public class LostFoundActivity extends BaseActivity1 implements View.OnClickListener {

    private SmartTabLayout mTabLayout;
    private ViewPager mViewPager;
    private SweetSheet mSweetSheet;
    private RelativeLayout rl;
    private FloatingActionButton fab;
    private int currentLostFoundType = 0;
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    @Override
    protected void initView() {
        setContentView(R.layout.activity_lost_found);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        rl = (RelativeLayout) findViewById(R.id.rl);
        mTabLayout = (SmartTabLayout) findViewById(R.id.viewpagertab);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ArrayList<MenuEntity> list = new ArrayList<>();
                //添加假数据

                MenuEntity menuEntity = new MenuEntity();
                menuEntity.iconId = R.drawable.lostfound_02;
                menuEntity.title = "新增寻物";

                MenuEntity menuEntity1 = new MenuEntity();
                menuEntity1.iconId = R.drawable.lostfound_01;
                menuEntity1.title = "发布招领";

                list.add(menuEntity);
                list.add(menuEntity1);
//                list.add(menuEntity2);
//                list.add(menuEntity3);
//                list.add(menuEntity4);
                // SweetSheet 控件,根据 rl 确认位置
                mSweetSheet = new SweetSheet(rl);

                //设置数据源 (数据源支持设置 list 数组,也支持从菜单中获取)
                mSweetSheet.setMenuList(list);
                //根据设置不同的 Delegate 来显示不同的风格.
                mSweetSheet.setDelegate(new RecyclerViewDelegate(false));
                //根据设置不同Effect 来显示背景效果BlurEffect:模糊效果.DimEffect 变暗效果
                mSweetSheet.setBackgroundEffect(new BlurEffect(8));
                //设置点击事件

                mSweetSheet.setOnMenuItemClickListener(new SweetSheet.OnMenuItemClickListener() {
                    @Override
                    public boolean onItemClick(int position, MenuEntity menuEntity1) {
                        switch (position) {
                            case 0:
                                if (!LoginUtils.isLogined(LostFoundActivity.this)) {
                                    LoginUtils.Login(LostFoundActivity.this);
                                } else {
                                    Intent lostIntent = new Intent(LostFoundActivity.this, AddLostFoundActivity.class);
                                    lostIntent.putExtra("LostFoundType", currentLostFoundType);
                                    lostIntent.putExtra("isLost", true);
                                    startActivity(lostIntent);
                                }
                                break;
                            case 1:
                                if (!LoginUtils.isLogined(LostFoundActivity.this)) {
                                    LoginUtils.Login(LostFoundActivity.this);
                                } else {
                                    Intent foundIntent = new Intent(LostFoundActivity.this, AddLostFoundActivity.class);
                                    foundIntent.putExtra("LostFoundType", currentLostFoundType);
                                    foundIntent.putExtra("isLost", false);
                                    startActivity(foundIntent);
                                }
                                break;
                            case 2:

                                break;
                            case 3:
                        }

                        return true;
                    }
                });
                mSweetSheet.toggle();
                mSweetSheet.setBgListener(new Delegate.BgListener() {
                    @Override
                    public void onClick() {
                        if (mSweetSheet.isShow()) {

                            mSweetSheet.dismiss();
                        }
                    }
                });


            }
        });


    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("失物招领");
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(LostFoundActivity.this)
                .add(getString(R.string.lostfound_book), BookFragment.class)
                .add(getString(R.string.lostfound_bag), BagFragment.class)
                .add(getString(R.string.lostfound_card), CardFragment.class)
                .add(getString(R.string.lostfound_digit), DigitFragment.class)
                .add(getString(R.string.lostfound_other), OtherFragment.class)


                .create());
        //mViewPager.setOffscreenPageLimit(10);
        if (mViewPager.getAdapter() == null) {
            mViewPager.setAdapter(adapter);
        }
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                currentLostFoundType = position;
                if (position > 0) {
                    EventBus.getDefault().post("", Constant.DISABLE_MENU);
                } else {
                    EventBus.getDefault().post("", Constant.ENABLE_MENU);
                }
                super.onPageSelected(position);
            }
        });
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        if (null == mSweetSheet) {
            super.onBackPressed();

        } else if (mSweetSheet.isShow()) {

            mSweetSheet.dismiss();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.titlebar_rl_back:
                finish();
                break;
        }
    }

    /*
    * 打开floatAction的动画
    * */
    private void openFloatActionBar() {
        YoYo.with(Techniques.ZoomInRight)
                .duration(700)
                .playOn(findViewById(R.id.fab));

    }    /*
    * 关闭floatAction的动画
    * */

    private void closeFloatActionBar() {
        YoYo.with(Techniques.ZoomOutRight)
                .duration(700)
                .playOn(findViewById(R.id.fab));

    }

}

